/* Copyright © 2010 LPRM
Universidade Federal do Espírito Santo - RNP-GT CWTools

This file is part of JoinUs!.

JoinUs! is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

JoinUs! is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with JoinUs!. If not, see <http://www.gnu.org/licenses/>. */
package android.isisufes.home2;

import java.io.IOException;
import java.util.Vector;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class Message {
	private String message, sender;

	Vector<Message> messages = new Vector<Message>();

	public static Message[] fromDataBase() {
		return null;
	} // TODO if needed

	public Vector<Message> fromXml(InputSource xml) {

		try {
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser sp;
			sp = spf.newSAXParser();
			XMLReader xr = sp.getXMLReader();
			MyHandler handler = new Message.MyHandler();
			xr.setContentHandler(handler);
			xr.setErrorHandler(handler);
			xr.parse(xml);
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}

		return messages;
	}

	class MyHandler extends DefaultHandler {
		String lastElement;

		boolean begin;

		boolean valid;

		int count = 0;

		public void endElement(String uri, String localName, String qName) {
			begin = false;
		}

		public void startElement(String uri, String localName, String qName,
				Attributes attributes) {
			begin = true;

			if (localName.compareTo("Message") == 0) {
				count++;
				messages.add(new Message());
				valid = true;
			}
			lastElement = localName;
			for (int i = 0; i < attributes.getLength(); i++) {
				lastElement = attributes.getValue(i);
				// Log.d("DEBUG","*******"+lastElement);
			}
		}

		public void characters(char[] ch, int start, int length) {
			if (valid) {
				String s = new String(ch, start, length);
				if (lastElement == null)
					return;
				if (!begin)
					return;
				if (lastElement.compareTo("sender") == 0) {
					if (s.compareTo("null") != 0) {
						// Log.d("DEBUG","id:"+s);
						Message commu = messages.get(count - 1);
						commu.sender = s;
					}
				} else if (lastElement.compareTo("message") == 0) {
					if (s.compareTo("null") != 0) {
						// Log.d("DEBUG","name:"+s);
						Message commu = messages.get(count - 1);
						commu.message = s;
					}
				} 
			}
		}
	}

	public String toXml() {
		return null;
	}// TODO

	public boolean toDataBase(SQLiteDatabase db, String user) {
		return true;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getSender() {
		return sender;
	}

	public void setSender(String sender) {
		this.sender = sender;
	}

	public void insertIntoDB(SQLiteDatabase db, String user){
		try {

				db
						.execSQL("INSERT INTO messages (msg, sender,user, id) values("
								+ "'"+this.message+"'"
								+ ", '"
								+ this.sender
								+ "', '"
								+ user + "', null);");

		} catch (SQLException e) {
			Log.d("DEBUG", "db 5 error");
		}
		
	}

	
}
